$(function(){
	layui.use(['form','laypage','util','layer'], function() {
					var form = layui.form;
					form.render();
					var laypage=layui.laypage;
					var util=layui.util;
					var layer=layui.layer;
					var total;//总条数
					var fileName1,fileName2,fileValue1,fileValue2;
					BaseUtil.ajaxSetup();
					function find_sysUser_list(page,rows,fileName1,fileValue1,fileName2,fileValue2){
						$.ajax({
							type:"Post",
							url:"http://localhost:8080/ssmService/sysUser/selectPageUserVo",
							async:false,
							data:{
								"queryParams.curr_page": page,
								"queryParams.page_size": rows,
								"baseModel.listAdvancedQuery[0].fieldName":fileName1,
								"baseModel.listAdvancedQuery[0].fieldValue":fileValue1,
								"baseModel.listAdvancedQuery[1].fieldName":fileName2,
								"baseModel.listAdvancedQuery[1].fieldValue":fileValue2
							},
							"success":function(result){
								if(result.resultCode==0){
									build_sysUser_list(result);//输出方法
									total=result.data.total;//输出分页
								}else{
									layer.msg("请求失败，联系管理员",{
										icon:2,
										time:1000
									});
									
								}							
							}
						});					
					}
					
				function build_sysUser_list(result){
					var userList=result.data.list;
					$(".table_tbody").empty();
					$.each(userList, function(index,item) {
						//存放id的目的是为了删除，或者修改单个记录的时候找到那条数据
						var tr=$("<tr></tr>");
						var td1=$("<td></td>").addClass("childrenBox").append("<input type='checkbox' user_id='"+item.user_id+"' lay-skin='primary'>");
						var td2=$("<td></td>").append(item.user.account);
						var td3=$("<td></td>").append(item.user.name);
						//判断phone
						var phone="";
						if(item.user.phone!=null){
							phone=item.user.phone
						}
						var td4=$("<td></td>").append(phone);
						//判断address
						var address="";
						if(item.user.address!=null){
							address=item.user.address
						}						
						var td5=$("<td></td>").append(address);
						//判断birthday
						var birthday="";
						if(item.user.birthday!=null){
							birthday=item.user.birthday
						}				
						var td6=$("<td></td>").append(util.toDateString(birthday,"yyyy-MM-dd HH:mm:ss"));
						
						//状态默认为1是开启状态
						var td7 = "";
						if(item.user.state == 1) {
							td7 = $("<td></td>").append("<input type='checkbox' user_id='" + item.user_id + "' lay-text='ON|OFF' lay-skin='switch' checked>");						
						}else{
							td7 = $("<td></td>").append("<input type='checkbox' user_id='" + item.user_id + "' lay-text='ON|OFF' lay-skin='switch'>");
						}							
						//var td7=$("<td></td>").append("<input type='checkbox' user_id='"+item.user_id+"'  lay-skin='switch' lay-text='ON|OFF'>");
						
						var td8=$("<td></td>").append("<button class='layui-btn layui-btn-sm updateUser' user_id='"+item.user_id+"'>编辑</button>")
						.append("<button class='layui-btn layui-btn-sm reset' user_id='"+item.user_id+"'>重置密码</button>")
						.append("<button class='layui-btn layui-btn-sm layui-btn-danger delete' user_id='"+item.user_id+"'>删除</button>");
						tr.append(td1).append(td2).append(td3).append(td4).append(td5).append(td6).append(td7).append(td8).appendTo(".table_tbody")
							});
						form.render();
					}
				
				

					//调用获取时获取用户列表的方法
					find_sysUser_list(1,10,null,null,null,null);
					
					$(".search").click(function(){
						//后台接收的值
						fileValue1=$("#account").val();
						fileValue2=$("#name").val();
						//后台接收的key，即为属性名
						fileName1="su.account";
						fileName2="su.name";
						find_sysUser_list(1, 10,fileName1,fileValue1,fileName2,fileValue2);
						laypage.render({
						elem: "table_page",
						layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
						count: total,
						jump: function(obj, first) {
							if(!first) {
								find_sysUser_list(obj.curr, obj.limit,fileName1,fileValue1,fileName2,fileValue2);
							}
						}
					});
				});
					//分页
					laypage.render({
					elem:"table_page",//绑定页面元素节点的id(只能是id)
					count:total,//数据总数，从服务器中读取中
					//分页的样式
					layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
					jump:function(obj,first){//当前分页执行成功后的回调函数
					//obj包含了当前分页的所有参数
					//obj.curr,obj.limit，当前页面所容纳的最大条数
					if(!first){
					find_sysUser_list(obj.curr,obj.limit,fileName1,fileValue1,fileName2,fileValue2);
					}
				}
			});			
			
			//删除
			$(document).on("click",".delete",function(){		
				var userId=$(this).attr("user_id");
				layer.confirm("您确定删除吗?",function(){//删除弹框
				delete_userByUserId(userId);
				},1000);
					});			
				
			function delete_userByUserId(userId){
				$.ajax({
					url:"http://localhost:8080/ssmService/sysUser/deleteUser/"+userId,
					async:true,
					data:{"ids":userId},	
					
					"success":function(result){
						if(result.resultCode==0){						
							layer.msg(result.message,{
								icon:1,
								time:1000
							});
							window.location.reload();
						}else{
							layer.msg(result.message,{
								icon:1,
								time:1000
										});
									}				
								}
							});				
						}	
						
					//全选
					form.on('checkbox(parentBox)',function(data){
					 if(data.elem.checked){
					 	 $(".childrenBox input[type='checkbox']").prop("checked",true);
					 	
					 }else{
					 	 $(".childrenBox input[type='checkbox']").prop("checked",false);				 	
					 }
						form.render();
					});				
					
					//反选
					form.on('checkbox',function(data){
						var length=0;	//checkbox总个数
						var checkLength=0;//被选中的总个数
						$(".childrenBox input[type='checkbox']").each(function(){
			 				length++;
				 			
				 			if($(this).is(":checked")){
				 				checkLength++;
				 			}
				 		});
						if(checkLength==length){
							$(".parentBox").prop("checked",true);
						}else{
							$(".parentBox").prop("checked",false);					
						}
						form.render();
					});
			
					//批量删除
					$(".batchDelete").click(function(){
						var ids="";//要删除的用户id
						var check=0;//记录删除的条数
						$(".childrenBox input[type='checkbox']").each(function(){
							//如果当前的checkbox被选中，则将userId的属性值存放到ids的变量中
							if($(this).is(":checked")){
								ids+=$(this).attr("user_id")+",";	
								check++;
								alert(ids);
							}							
						});
						//若选中次数不超过一次，给出错误提示
						if(check>0){						
						layer.confirm("你确定要删除"+check+"条记录吗？",function(){
							//将最后一位的逗号给删除
							delete_userByUserId(ids.substring(0,ids.length-1));
							});							
						}else{
							layer.msg("不能少于0条记录",{
							icon:2,
							time:1000				
							})
						}	
				});	
				
				//账号的开启与禁封
				form.on("switch",function(data){
					var userId=data.elem.getAttribute("user_id");				
					 change_state(userId);
				});
				
				//修改用户状态
				function change_state(userId){
					$.ajax({
						type:"get",
						url:"http://localhost:8080/ssmService/sysUser/tiggerState/" + userId,
						async:true,					
						"success":function(result){
						if(result.resultCode==0){						
							layer.msg(result.message,{
								icon:1,
								time:1000
							});																
						}						
					},				
				"error":function(){
					layer.msg("状态切换失败，请联系管理员",{
						icon:2,
						time:1000
							});
						}						
					});
					
				}
				
				//重置密码
				$(document).on("click",".reset",function(){
					var userId=$(this).attr("user_id");	
					reset_password(userId);
				});
				
				function reset_password(userId){
					$.ajax({
						type:"get",
						url:"http://localhost:8080/ssmService/sysUser/resetPassword/" + userId,
						async:true,					
						"success":function(result){
						if(result.resultCode==0){						
							layer.msg(result.message,{
								icon:1,
								time:1000
							});																
						}						
					},
					
				"error":function(){
					layer.msg("重置密码失败，请联系管理员",{
						icon:2,
						time:1000
							});
						}						
					});
					
				}
				
				
				//点击添加用户全屏
					$(".addUser").click(function() {
						var index = layer.open({
							type: 2,
							title: false,
							shadeClose: true,
							shade:0.8,					
							content: 'addUser.html',
							
						});
						layer.full(index);
					});
				
				//修改用户
				$(document).on("click",".updateUser",function(){	
					    var userId=$(this).attr("user_id");
						//点击修改用户全屏				
						var index = layer.open({
							type: 2,
							title:'修改用户',
							shadeClose: true,
							shade:0.8,	
							content: 'updateUser.html?userId='+userId,
						});
						layer.full(index);
					});	
					
			});	
});	
